home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: johnb@pivotal-dm.ccmail.compuserve.com (John Bain)
- Newsgroups: comp.lang.c++
- Subject: Re: Borland C++ 4.5 : delete [] operator - what's going on here?
- Date: Tue, 06 Feb 1996 12:21:14 GMT
- Organization: Pivotal Technologies
- Message-ID: <31171e2d.7861133@dub-news-svc-3.compuserve.com>
- References: <4espam$ddf@rks1.urz.tu-dresden.de>
- NNTP-Posting-Host: dd62-138.compuserve.com
- X-Newsreader: Forte Agent .99c/16.141
-
- Hoang Minh Son <hoang@eatns1.et.tu-dresden.de> wrote:
-
- >template<class T>
- >TMatrix<T>::TMatrix(size_e m, size_t n) : nrow(m), ncol(n)
- >{
- > elem = new T[m*n+1]; // a dummy space for efficient use
- > pcol = new T*[n+1]; // of 1-based indexing
-
- I.e. pcol points to an array of n+1 T*'s, so pcol[x] is valid
- for 0<=x<=n.
-
- >
- > if (n > 0)
- > {
- > pcol[1] = elem;
- > for (int i=1; i <= n; i++)
- > pcol[i+1] = pcol[i] + m;
- > }
- >}
-
- The for loop's last write is to pcol[n+1] - outside the allocated range.
-
- You probably want
-
- for (int i=1; i < n; i++)
- pcol[i+1] = pcol[i] + m;
-
- or, preferably (IMHO)
-
- for (int i=2; i <= n; i++)
- pcol[i] = pcol[i-1] + m;
-
- Regards,
-
- John
- -----------------------------------------------------------------
- John Bain
- johnb@pivotal-dm.ccmail.compuserve.com
- -----------------------------------------------------------------
-
-